package com.wuzheng.wisgo.sanitation.cloud.controller.plan;

import com.wuzheng.wisgo.sanitation.cloud.model.common.param.DeleteParam;
import com.wuzheng.wisgo.sanitation.cloud.model.plan.param.*;
import com.wuzheng.wisgo.sanitation.cloud.model.plan.pojo.JobPlanDTO;
import com.wuzheng.wisgo.sanitation.cloud.model.plan.pojo.JobPlanDetailRouteDTO;
import com.wuzheng.wisgo.sanitation.cloud.model.plan.pojo.JobPlanDetailShiftDTO;
import com.wuzheng.wisgo.sanitation.cloud.model.plan.pojo.JobPlanVehicleDTO;
import com.wuzheng.wisgo.sanitation.cloud.repository.entity.person.User;
import com.wuzheng.wisgo.sanitation.cloud.repository.entity.plan.JobPlan;
import com.wuzheng.wisgo.sanitation.cloud.service.plan.JobPlanService;
import com.wuzheng.wisgo.sanitation.cloud.service.plan.JobPlanShiftService;
import com.wuzheng.wisgo.sanitation.cloud.service.plan.JobPlanVehicleService;
import com.wuzheng.wisgo.sanitation.framework.base.annotation.CurrentUser;
import com.wuzheng.wisgo.sanitation.framework.base.controller.BaseController;
import com.wuzheng.wisgo.sanitation.framework.core.constant.SysRespEnum;
import com.wuzheng.wisgo.sanitation.framework.core.protocol.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author：hoipang
 * @date：Created in 2019-12-18
 * @description：作业计划控制器
 * @modified By：
 * @version: 1.0
 */
@RestController
@RequestMapping("/jobPlan")
@Api(value = "JobPlanCtrl", tags = "作业计划接口")
public class JobPlanCtrl extends BaseController {
    @Resource
    JobPlanService jobPlanService;
    @Resource
    JobPlanShiftService jobPlanShiftService;
    @Resource
    JobPlanVehicleService jobPlanVehicleService;

    /**
     * 获取作业计划列表
     */
    @PostMapping(value = "/list")
    @ApiOperation(value = "获取作业计划列表")
    public Result<List<JobPlanDTO>> list(@RequestBody JobPlanListParam jobPlanListParam) {
        return this.success(SysRespEnum.SUCCESS.getMessage(), jobPlanService.listJobPlanDTOs(jobPlanListParam));
    }

    /**
     * 获取作业计划详情中的班次
     */
    @PostMapping(value = "/shifts")
    @ApiOperation(value = "获取作业计划详情中的班次")
    public Result<List<JobPlanDetailShiftDTO>> listShifts(
            @RequestBody @Validated JobPlanDetailTypeParam jobPlanDetailTypeParam,
            BindingResult bindingResult) throws BindException {
        if (bindingResult.hasErrors()) {
            throw new BindException(bindingResult);
        }
        return this.success(SysRespEnum.SUCCESS.getMessage(),
                jobPlanShiftService.listDetailShiftDTOs(jobPlanDetailTypeParam.getJobTypeCode()));
    }

    /**
     * 获取作业计划详情中的线路
     */
    @PostMapping(value = "/routes")
    @ApiOperation(value = "获取作业计划详情中的线路")
    public Result<List<JobPlanDetailRouteDTO>> listRoutes(
            @RequestBody @Validated JobPlanDetailTypeParam jobPlanDetailTypeParam,
            BindingResult bindingResult) throws BindException {
        if (bindingResult.hasErrors()) {
            throw new BindException(bindingResult);
        }
        return this.success(SysRespEnum.SUCCESS.getMessage(),
                jobPlanService.listDetailRouteDTOs(jobPlanDetailTypeParam.getJobTypeCode()));
    }

    /**
     * 获取作业计划详情中的车辆
     */
    @PostMapping(value = "/vehicles")
    @ApiOperation(value = "获取作业计划详情中的车辆")
    public Result<List<JobPlanVehicleDTO>> listVehicles(
            @RequestBody @Validated JobPlanDetailVehicleParam jobPlanDetailVehicleParam,
            BindingResult bindingResult) throws BindException {
        if (bindingResult.hasErrors()) {
            throw new BindException(bindingResult);
        }
        return this.success(SysRespEnum.SUCCESS.getMessage(),
                jobPlanVehicleService.listDetailVehicleDTOs(jobPlanDetailVehicleParam));
    }

    /**
     * 删除作业计划(支持批量)
     */
    @PostMapping(value = "/delete")
    @ApiOperation(value = "删除作业计划(支持批量)")
    public Result<String> delete(@RequestBody DeleteParam deleteParam) {
        jobPlanService.removeJobPlans(deleteParam.getIds());
        return this.success(SysRespEnum.SUCCESS.getMessage());
    }

    /**
     * 获取作业计划详情
     */
    @PostMapping(value = "/info")
    @ApiOperation(value = "获取作业计划详情")
    public Result<JobPlan> get(@RequestBody @Validated JobPlanDetailParam jobPlanDetailparam,
                               BindingResult bindingResult) throws BindException {
        if (bindingResult.hasErrors()) {
            throw new BindException(bindingResult);
        }
        return this.success(SysRespEnum.SUCCESS.getMessage(), jobPlanService.selectJobPlanDetailDTO(jobPlanDetailparam));
    }

    /**
     * 保存作业计划
     */
    @PostMapping(value = "/save")
    @ApiOperation(value = "保存道路作业计划")
    public Result<String> insertOrUpdate(@ApiIgnore @CurrentUser User user,
                                         @RequestBody @Validated JobPlanSaveParam jobPlanSaveParam,
                                         BindingResult bindingResult) throws BindException {
        if (bindingResult.hasErrors()) {
            throw new BindException(bindingResult);
        }
        return this.success(SysRespEnum.SUCCESS.getMessage(), jobPlanService.insertOrUpdate(user, jobPlanSaveParam));
    }
}

